package com.yourenbang.lingyun.transaction.web.service.impl.yxy;

import com.yourenbang.lingyun.constants.enums.ChannelIdEnums;
import com.yourenbang.lingyun.core.config.datasource.DataSources;
import com.yourenbang.lingyun.core.config.datasource.RoutingDataSource;
import com.yourenbang.lingyun.transaction.config.AcmGlobalParams;
import com.yourenbang.lingyun.transaction.job.common.log.XxlJobLogger;
import com.yourenbang.lingyun.transaction.mapper.TransactionPayTransactionIndexMapper;
import com.yourenbang.lingyun.transaction.util.DateUtil;
import com.yourenbang.lingyun.transaction.web.domain.dto.yxy.TransactionCountYxyDTO;
import com.yourenbang.lingyun.transaction.web.service.yxy.JobTransactionYxyService;
import com.yourenbang.mcenter.api.model.dto.dingding.SendDingDingDTO;
import com.yourenbang.mcenter.api.service.dingding.McenterRpcDingDingService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Service
public class JobTransactionYxyServiceImpl implements JobTransactionYxyService {

    @Resource
    AcmGlobalParams acmGlobalParams;
    @Resource
    TransactionPayTransactionIndexMapper transactionPayTransactionIndexMapper;
    @Resource
    McenterRpcDingDingService mcenterRpcDingDingService;

    @Override
    @RoutingDataSource(DataSources.SLAVE_DB)
    public void pushTransactionYxy() {
        XxlJobLogger.log("【钉钉推送银小云交易统计线程】开始执行时间:{}", DateUtil.getDate());

        //P40、P42、P45、S40、L40、基梯、昨日交易总额
        BigDecimal dayAmountP40 = new BigDecimal(0);
        BigDecimal dayAmountP42 = new BigDecimal(0);
        BigDecimal dayAmountP45 = new BigDecimal(0);
        BigDecimal dayAmountS40 = new BigDecimal(0);
        BigDecimal dayAmountL40 = new BigDecimal(0);
        BigDecimal dayAmountD40 = new BigDecimal(0);
        BigDecimal dayAmountM40 = new BigDecimal(0);
        BigDecimal dayAmountK40 = new BigDecimal(0);
        BigDecimal dayAmountY40 = new BigDecimal(0);
        BigDecimal dayAmountP50 = new BigDecimal(0);
        BigDecimal dayAmountS50 = new BigDecimal(0);
        BigDecimal dayAmountL50 = new BigDecimal(0);
        BigDecimal dayAmountD50 = new BigDecimal(0);
        BigDecimal dayAmountD55 = new BigDecimal(0);
        BigDecimal dayAmountY50 = new BigDecimal(0);
        BigDecimal dayAmountZ50 = new BigDecimal(0);
        BigDecimal dayAmountH55 = new BigDecimal(0);
        //P40、P42、P45、S40、L40、基梯、昨日交易笔数
        Integer dayCountP40 = 0;
        Integer dayCountP42 = 0;
        Integer dayCountP45 = 0;
        Integer dayCountS40 = 0;
        Integer dayCountL40 = 0;
        Integer dayCountD40 = 0;
        Integer dayCountM40 = 0;
        Integer dayCountK40 = 0;
        Integer dayCountY40 = 0;
        Integer dayCountP50 = 0;
        Integer dayCountS50 = 0;
        Integer dayCountL50 = 0;
        Integer dayCountD50 = 0;
        Integer dayCountD55 = 0;
        Integer dayCountY50 = 0;
        Integer dayCountZ50 = 0;
        Integer dayCountH55 = 0;
        //P40、S40、L40昨日交易商户数
        Integer merchantDayCountP40 = 0;
        Integer merchantDayCountP42 = 0;
        Integer merchantDayCountP45 = 0;
        Integer merchantDayCountS40 = 0;
        Integer merchantDayCountL40 = 0;
        Integer merchantDayCountD40 = 0;
        Integer merchantDayCountM40 = 0;
        Integer merchantDayCountK40 = 0;
        Integer merchantDayCountY40 = 0;
        Integer merchantDayCountP50 = 0;
        Integer merchantDayCountS50 = 0;
        Integer merchantDayCountL50 = 0;
        Integer merchantDayCountD50 = 0;
        Integer merchantDayCountD55 = 0;
        Integer merchantDayCountY50 = 0;
        Integer merchantDayCountZ50 = 0;
        Integer merchantDayCountH55 = 0;

        //昨日时间、前日时间
        DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        String yesterday = LocalDateTime.now().minusDays(1).format(fmt);
        String last2Day = LocalDateTime.now().minusDays(2).format(fmt);
        String startDayTime = yesterday.concat(" 00:00:00");
        String endDayTime = yesterday.concat(" 23:59:59");
        String last2DayStartDayTime = last2Day.concat(" 00:00:00");
        String last2DayEndDayTime = last2Day.concat(" 23:59:59");

        //银小云昨日交易额
        List<TransactionCountYxyDTO> transactionYesterday = transactionPayTransactionIndexMapper.getTransaction(startDayTime, endDayTime);

        for (TransactionCountYxyDTO transactionCountYxyDTO : transactionYesterday) {
            if (transactionCountYxyDTO.getChannelId().shortValue() == ChannelIdEnums.yxy_lkl_p40.getCode().shortValue()) {
                dayAmountP40 = transactionCountYxyDTO.getAmount();
                dayCountP40 = transactionCountYxyDTO.getCount();
                merchantDayCountP40 = transactionCountYxyDTO.getMerchantCount();
            } else if (transactionCountYxyDTO.getChannelId().shortValue() == ChannelIdEnums.yxy_lkl_p42.getCode().shortValue()) {
                dayAmountP42 = transactionCountYxyDTO.getAmount();
                dayCountP42 = transactionCountYxyDTO.getCount();
                merchantDayCountP42 = transactionCountYxyDTO.getMerchantCount();
            } else if (transactionCountYxyDTO.getChannelId().shortValue() == ChannelIdEnums.yxy_lkl_p45.getCode().shortValue()) {
                dayAmountP45 = transactionCountYxyDTO.getAmount();
                dayCountP45 = transactionCountYxyDTO.getCount();
                merchantDayCountP45 = transactionCountYxyDTO.getMerchantCount();
            } else if (transactionCountYxyDTO.getChannelId().shortValue() == ChannelIdEnums.yxy_sft_s40.getCode().shortValue()) {
                dayAmountS40 = transactionCountYxyDTO.getAmount();
                dayCountS40 = transactionCountYxyDTO.getCount();
                merchantDayCountS40 = transactionCountYxyDTO.getMerchantCount();
            } else if (transactionCountYxyDTO.getChannelId().shortValue() == ChannelIdEnums.yxy_ls_l40.getCode().shortValue()) {
                dayAmountL40 = transactionCountYxyDTO.getAmount();
                dayCountL40 = transactionCountYxyDTO.getCount();
                merchantDayCountL40 = transactionCountYxyDTO.getMerchantCount();
            } else if (transactionCountYxyDTO.getChannelId().shortValue() == ChannelIdEnums.yxy_dy_d40.getCode().shortValue()) {
                dayAmountD40 = transactionCountYxyDTO.getAmount();
                dayCountD40 = transactionCountYxyDTO.getCount();
                merchantDayCountD40 = transactionCountYxyDTO.getMerchantCount();
            } else if (transactionCountYxyDTO.getChannelId().shortValue() == ChannelIdEnums.k40.getCode().shortValue()) {
                dayAmountK40 = transactionCountYxyDTO.getAmount();
                dayCountK40 = transactionCountYxyDTO.getCount();
                merchantDayCountK40 = transactionCountYxyDTO.getMerchantCount();
            }
        }

        //基梯昨日交易额
        List<TransactionCountYxyDTO> transactionJTYesterday = transactionPayTransactionIndexMapper.getTransactionJT(startDayTime, endDayTime);
        BigDecimal dayAmountJT = transactionJTYesterday.get(0).getAmount() == null ? BigDecimal.ZERO : transactionJTYesterday.get(0).getAmount();
        Integer dayCountJT = transactionJTYesterday.get(0).getCount();

        //基梯前日交易额
        List<TransactionCountYxyDTO> transactionLast2Days = transactionPayTransactionIndexMapper.getTransaction(last2DayStartDayTime, last2DayEndDayTime);
        for (TransactionCountYxyDTO transactionLast2Day : transactionLast2Days) {
            if (transactionLast2Day.getChannelId().shortValue() == ChannelIdEnums.yxy_jl_m40.getCode().shortValue()) {
                dayAmountM40 = transactionLast2Day.getAmount();
                dayCountM40 = transactionLast2Day.getCount();
                merchantDayCountM40 = transactionLast2Day.getMerchantCount();
            }
        }

        //昨日平均每笔交易额
        BigDecimal dayAvgAmountP40 = dayCountP40 == 0 ? new BigDecimal(0) : dayAmountP40.divide(new BigDecimal(dayCountP40), 2, RoundingMode.HALF_UP);
        BigDecimal dayAvgAmountP42 = dayCountP42 == 0 ? new BigDecimal(0) : dayAmountP42.divide(new BigDecimal(dayCountP42), 2, RoundingMode.HALF_UP);
        BigDecimal dayAvgAmountP45 = dayCountP45 == 0 ? new BigDecimal(0) : dayAmountP45.divide(new BigDecimal(dayCountP45), 2, RoundingMode.HALF_UP);
        BigDecimal dayAvgAmountS40 = dayCountS40 == 0 ? new BigDecimal(0) : dayAmountS40.divide(new BigDecimal(dayCountS40), 2, RoundingMode.HALF_UP);
        BigDecimal dayAvgAmountL40 = dayCountL40 == 0 ? new BigDecimal(0) : dayAmountL40.divide(new BigDecimal(dayCountL40), 2, RoundingMode.HALF_UP);
        BigDecimal dayAvgAmountD40 = dayCountD40 == 0 ? new BigDecimal(0) : dayAmountD40.divide(new BigDecimal(dayCountD40), 2, RoundingMode.HALF_UP);
        BigDecimal dayAvgAmountM40 = dayCountM40 == 0 ? new BigDecimal(0) : dayAmountM40.divide(new BigDecimal(dayCountM40), 2, RoundingMode.HALF_UP);
        BigDecimal dayAvgAmountK40 = dayCountK40 == 0 ? new BigDecimal(0) : dayAmountK40.divide(new BigDecimal(dayCountK40), 2, RoundingMode.HALF_UP);
        BigDecimal dayAvgAmountY40 = dayCountY40 == 0 ? new BigDecimal(0) : dayAmountY40.divide(new BigDecimal(dayCountY40), 2, RoundingMode.HALF_UP);
        BigDecimal dayAvgAmountP50 = dayCountP50 == 0 ? new BigDecimal(0) : dayAmountP50.divide(new BigDecimal(dayCountP50), 2, RoundingMode.HALF_UP);
        BigDecimal dayAvgAmountS50 = dayCountS50 == 0 ? new BigDecimal(0) : dayAmountS50.divide(new BigDecimal(dayCountS50), 2, RoundingMode.HALF_UP);
        BigDecimal dayAvgAmountL50 = dayCountL50 == 0 ? new BigDecimal(0) : dayAmountL50.divide(new BigDecimal(dayCountL50), 2, RoundingMode.HALF_UP);
        BigDecimal dayAvgAmountD50 = dayCountD50 == 0 ? new BigDecimal(0) : dayAmountD50.divide(new BigDecimal(dayCountD50), 2, RoundingMode.HALF_UP);
        BigDecimal dayAvgAmountD55 = dayCountD55 == 0 ? new BigDecimal(0) : dayAmountD55.divide(new BigDecimal(dayCountD55), 2, RoundingMode.HALF_UP);
        BigDecimal dayAvgAmountY50 = dayCountY50 == 0 ? new BigDecimal(0) : dayAmountY50.divide(new BigDecimal(dayCountY50), 2, RoundingMode.HALF_UP);
        BigDecimal dayAvgAmountZ50 = dayCountZ50 == 0 ? new BigDecimal(0) : dayAmountZ50.divide(new BigDecimal(dayCountZ50), 2, RoundingMode.HALF_UP);
        BigDecimal dayAvgAmountH55 = dayCountH55 == 0 ? new BigDecimal(0) : dayAmountH55.divide(new BigDecimal(dayCountH55), 2, RoundingMode.HALF_UP);

        //昨日平均客户交易笔数
        Integer dayAvgCountP40 = merchantDayCountP40 == 0 ? 0 : dayCountP40 / merchantDayCountP40;
        Integer dayAvgCountP42 = merchantDayCountP42 == 0 ? 0 : dayCountP42 / merchantDayCountP42;
        Integer dayAvgCountP45 = merchantDayCountP45 == 0 ? 0 : dayCountP45 / merchantDayCountP45;
        Integer dayAvgCountS40 = merchantDayCountS40 == 0 ? 0 : dayCountS40 / merchantDayCountS40;
        Integer dayAvgCountL40 = merchantDayCountL40 == 0 ? 0 : dayCountL40 / merchantDayCountL40;
        Integer dayAvgCountD40 = merchantDayCountD40 == 0 ? 0 : dayCountD40 / merchantDayCountD40;
        Integer dayAvgCountM40 = merchantDayCountM40 == 0 ? 0 : dayCountM40 / merchantDayCountM40;
        Integer dayAvgCountK40 = merchantDayCountK40 == 0 ? 0 : dayCountK40 / merchantDayCountK40;
        Integer dayAvgCountY40 = merchantDayCountY40 == 0 ? 0 : dayCountY40 / merchantDayCountY40;
        Integer dayAvgCountP50 = merchantDayCountP50 == 0 ? 0 : dayCountP50 / merchantDayCountP50;
        Integer dayAvgCountS50 = merchantDayCountS50 == 0 ? 0 : dayCountS50 / merchantDayCountS50;
        Integer dayAvgCountL50 = merchantDayCountL50 == 0 ? 0 : dayCountL50 / merchantDayCountL50;
        Integer dayAvgCountD50 = merchantDayCountD50 == 0 ? 0 : dayCountD50 / merchantDayCountD50;
        Integer dayAvgCountD55 = merchantDayCountD55 == 0 ? 0 : dayCountD55 / merchantDayCountD55;
        Integer dayAvgCountY50 = merchantDayCountY50 == 0 ? 0 : dayCountY50 / merchantDayCountY50;
        Integer dayAvgCountZ50 = merchantDayCountZ50 == 0 ? 0 : dayCountZ50 / merchantDayCountZ50;
        Integer dayAvgCountH55 = merchantDayCountH55 == 0 ? 0 : dayCountH55 / merchantDayCountH55;

        //月度统计
        //P42、S40、L40、基梯、本月交易总额
        BigDecimal monthAmountP40 = new BigDecimal(0);
        BigDecimal monthAmountP42 = new BigDecimal(0);
        BigDecimal monthAmountP45 = new BigDecimal(0);
        BigDecimal monthAmountS40 = new BigDecimal(0);
        BigDecimal monthAmountL40 = new BigDecimal(0);
        BigDecimal monthAmountD40 = new BigDecimal(0);
        BigDecimal monthAmountM40 = new BigDecimal(0);
        BigDecimal monthAmountK40 = new BigDecimal(0);
        BigDecimal monthAmountY40 = new BigDecimal(0);
        BigDecimal monthAmountP50 = new BigDecimal(0);
        BigDecimal monthAmountS50 = new BigDecimal(0);
        BigDecimal monthAmountL50 = new BigDecimal(0);
        BigDecimal monthAmountD50 = new BigDecimal(0);
        BigDecimal monthAmountD55 = new BigDecimal(0);
        BigDecimal monthAmountY50 = new BigDecimal(0);
        BigDecimal monthAmountZ50 = new BigDecimal(0);
        BigDecimal monthAmountH55 = new BigDecimal(0);
        BigDecimal monthAmountJT;
        //P40、S40、L40、基梯、本月交易笔数
        Integer monthCountP40 = 0;
        Integer monthCountP42 = 0;
        Integer monthCountP45 = 0;
        Integer monthCountS40 = 0;
        Integer monthCountL40 = 0;
        Integer monthCountD40 = 0;
        Integer monthCountM40 = 0;
        Integer monthCountK40 = 0;
        Integer monthCountY40 = 0;
        Integer monthCountP50 = 0;
        Integer monthCountS50 = 0;
        Integer monthCountL50 = 0;
        Integer monthCountD50 = 0;
        Integer monthCountD55 = 0;
        Integer monthCountY50 = 0;
        Integer monthCountZ50 = 0;
        Integer monthCountH55 = 0;
        Integer monthCountJT;
        //P40、S40、L40本月交易商户数
        Integer merchantMonthCountP40 = 0;
        Integer merchantMonthCountP42 = 0;
        Integer merchantMonthCountP45 = 0;
        Integer merchantMonthCountS40 = 0;
        Integer merchantMonthCountL40 = 0;
        Integer merchantMonthCountD40 = 0;
        Integer merchantMonthCountM40 = 0;
        Integer merchantMonthCountK40 = 0;
        Integer merchantMonthCountY40 = 0;
        Integer merchantMonthCountP50 = 0;
        Integer merchantMonthCountS50 = 0;
        Integer merchantMonthCountL50 = 0;
        Integer merchantMonthCountD50 = 0;
        Integer merchantMonthCountD55 = 0;
        Integer merchantMonthCountY50 = 0;
        Integer merchantMonthCountZ50 = 0;
        Integer merchantMonthCountH55 = 0;

        //本月起始时间
        String firstMonthDay = LocalDateTime.now().with(TemporalAdjusters.firstDayOfMonth()).format(fmt);
        String startMonthTime = firstMonthDay.concat(" 00:00:00");
        String endMonthTime = LocalDate.now().toString().concat(" 23:59:59");
        List<TransactionCountYxyDTO> transactionJTCurrentMonth = transactionPayTransactionIndexMapper.getTransactionJT(startMonthTime, endMonthTime);
        monthAmountJT = transactionJTCurrentMonth.get(0).getAmount() == null ? BigDecimal.ZERO : transactionJTCurrentMonth.get(0).getAmount();
        monthCountJT = transactionJTCurrentMonth.get(0).getCount();
        List<TransactionCountYxyDTO> transactionCurrentMonth = transactionPayTransactionIndexMapper.getTransaction(startMonthTime, endMonthTime);
        for (TransactionCountYxyDTO transactionCountYxyDTO : transactionCurrentMonth) {
            if (transactionCountYxyDTO.getChannelId().shortValue() == ChannelIdEnums.yxy_lkl_p40.getCode().shortValue()) {
                monthAmountP40 = transactionCountYxyDTO.getAmount();
                monthCountP40 = transactionCountYxyDTO.getCount();
                merchantMonthCountP40 = transactionCountYxyDTO.getMerchantCount();
            } else if (transactionCountYxyDTO.getChannelId().shortValue() == ChannelIdEnums.yxy_lkl_p42.getCode().shortValue()) {
                monthAmountP42 = transactionCountYxyDTO.getAmount();
                monthCountP42 = transactionCountYxyDTO.getCount();
                merchantMonthCountP42 = transactionCountYxyDTO.getMerchantCount();
            } else if (transactionCountYxyDTO.getChannelId().shortValue() == ChannelIdEnums.yxy_lkl_p45.getCode().shortValue()) {
                monthAmountP45 = transactionCountYxyDTO.getAmount();
                monthCountP45 = transactionCountYxyDTO.getCount();
                merchantMonthCountP45 = transactionCountYxyDTO.getMerchantCount();
            } else if (transactionCountYxyDTO.getChannelId().shortValue() == ChannelIdEnums.yxy_sft_s40.getCode().shortValue()) {
                monthAmountS40 = transactionCountYxyDTO.getAmount();
                monthCountS40 = transactionCountYxyDTO.getCount();
                merchantMonthCountS40 = transactionCountYxyDTO.getMerchantCount();
            } else if (transactionCountYxyDTO.getChannelId().shortValue() == ChannelIdEnums.yxy_ls_l40.getCode().shortValue()) {
                monthAmountL40 = transactionCountYxyDTO.getAmount();
                monthCountL40 = transactionCountYxyDTO.getCount();
                merchantMonthCountL40 = transactionCountYxyDTO.getMerchantCount();
            } else if (transactionCountYxyDTO.getChannelId().shortValue() == ChannelIdEnums.yxy_dy_d40.getCode().shortValue()) {
                monthAmountD40 = transactionCountYxyDTO.getAmount();
                monthCountD40 = transactionCountYxyDTO.getCount();
                merchantMonthCountD40 = transactionCountYxyDTO.getMerchantCount();
            } else if (transactionCountYxyDTO.getChannelId().shortValue() == ChannelIdEnums.yxy_jl_m40.getCode().shortValue()) {
                monthAmountM40 = transactionCountYxyDTO.getAmount();
                monthCountM40 = transactionCountYxyDTO.getCount();
                merchantMonthCountM40 = transactionCountYxyDTO.getMerchantCount();
            } else if (transactionCountYxyDTO.getChannelId().shortValue() == ChannelIdEnums.k40.getCode().shortValue()) {
                monthAmountK40 = transactionCountYxyDTO.getAmount();
                monthCountK40 = transactionCountYxyDTO.getCount();
                merchantMonthCountK40 = transactionCountYxyDTO.getMerchantCount();
            }
            else if (transactionCountYxyDTO.getChannelId().shortValue() == ChannelIdEnums.yxy_ldys_y40.getCode().shortValue()) { monthAmountY40 = transactionCountYxyDTO.getAmount(); monthCountY40 = transactionCountYxyDTO.getCount(); merchantMonthCountY40 = transactionCountYxyDTO.getMerchantCount(); }
            else if (transactionCountYxyDTO.getChannelId().shortValue() == ChannelIdEnums.YXY_LKL_P50.getCode().shortValue()) { monthAmountP50 = transactionCountYxyDTO.getAmount(); monthCountP50 = transactionCountYxyDTO.getCount(); merchantMonthCountP50 = transactionCountYxyDTO.getMerchantCount(); }
            else if (transactionCountYxyDTO.getChannelId().shortValue() == ChannelIdEnums.YXY_SFT_S50.getCode().shortValue()) { monthAmountS50 = transactionCountYxyDTO.getAmount(); monthCountS50 = transactionCountYxyDTO.getCount(); merchantMonthCountS50 = transactionCountYxyDTO.getMerchantCount(); }
            else if (transactionCountYxyDTO.getChannelId().shortValue() == ChannelIdEnums.YXY_LS_L50.getCode().shortValue()) { monthAmountL50 = transactionCountYxyDTO.getAmount(); monthCountL50 = transactionCountYxyDTO.getCount(); merchantMonthCountL50 = transactionCountYxyDTO.getMerchantCount(); }
            else if (transactionCountYxyDTO.getChannelId().shortValue() == ChannelIdEnums.YXY_DY_D50.getCode().shortValue()) { monthAmountD50 = transactionCountYxyDTO.getAmount(); monthCountD50 = transactionCountYxyDTO.getCount(); merchantMonthCountD50 = transactionCountYxyDTO.getMerchantCount(); }
            else if (transactionCountYxyDTO.getChannelId().shortValue() == ChannelIdEnums.YXY_DY_D55.getCode().shortValue()) { monthAmountD55 = transactionCountYxyDTO.getAmount(); monthCountD55 = transactionCountYxyDTO.getCount(); merchantMonthCountD55 = transactionCountYxyDTO.getMerchantCount(); }
            else if (transactionCountYxyDTO.getChannelId().shortValue() == ChannelIdEnums.YXY_LDYS_Y50.getCode().shortValue()) { monthAmountY50 = transactionCountYxyDTO.getAmount(); monthCountY50 = transactionCountYxyDTO.getCount(); merchantMonthCountY50 = transactionCountYxyDTO.getMerchantCount(); }
            else if (transactionCountYxyDTO.getChannelId().shortValue() == ChannelIdEnums.YXY_LS_Z50.getCode().shortValue()) { monthAmountZ50 = transactionCountYxyDTO.getAmount(); monthCountZ50 = transactionCountYxyDTO.getCount(); merchantMonthCountZ50 = transactionCountYxyDTO.getMerchantCount(); }
            else if (transactionCountYxyDTO.getChannelId().shortValue() == ChannelIdEnums.YXY_HK_H55.getCode().shortValue()) { monthAmountH55 = transactionCountYxyDTO.getAmount(); monthCountH55 = transactionCountYxyDTO.getCount(); merchantMonthCountH55 = transactionCountYxyDTO.getMerchantCount(); }
        }

        //本月平均每笔交易额
        BigDecimal monthAvgAmountP40 = monthCountP40 == 0 ? new BigDecimal(0) : monthAmountP40.divide(new BigDecimal(monthCountP40), 2, RoundingMode.HALF_UP);
        BigDecimal monthAvgAmountP42 = monthCountP42 == 0 ? new BigDecimal(0) : monthAmountP42.divide(new BigDecimal(monthCountP42), 2, RoundingMode.HALF_UP);
        BigDecimal monthAvgAmountP45 = monthCountP45 == 0 ? new BigDecimal(0) : monthAmountP45.divide(new BigDecimal(monthCountP45), 2, RoundingMode.HALF_UP);
        BigDecimal monthAvgAmountS40 = monthCountS40 == 0 ? new BigDecimal(0) : monthAmountS40.divide(new BigDecimal(monthCountS40), 2, RoundingMode.HALF_UP);
        BigDecimal monthAvgAmountL40 = monthCountL40 == 0 ? new BigDecimal(0) : monthAmountL40.divide(new BigDecimal(monthCountL40), 2, RoundingMode.HALF_UP);
        BigDecimal monthAvgAmountD40 = monthCountD40 == 0 ? new BigDecimal(0) : monthAmountD40.divide(new BigDecimal(monthCountD40), 2, RoundingMode.HALF_UP);
        BigDecimal monthAvgAmountM40 = monthCountM40 == 0 ? new BigDecimal(0) : monthAmountM40.divide(new BigDecimal(monthCountM40), 2, RoundingMode.HALF_UP);
        BigDecimal monthAvgAmountK40 = monthCountK40 == 0 ? new BigDecimal(0) : monthAmountK40.divide(new BigDecimal(monthCountK40), 2, RoundingMode.HALF_UP);
        BigDecimal monthAvgAmountY40 = monthCountY40 == 0 ? new BigDecimal(0) : monthAmountY40.divide(new BigDecimal(monthCountY40), 2, RoundingMode.HALF_UP);
        BigDecimal monthAvgAmountP50 = monthCountP50 == 0 ? new BigDecimal(0) : monthAmountP50.divide(new BigDecimal(monthCountP50), 2, RoundingMode.HALF_UP);
        BigDecimal monthAvgAmountS50 = monthCountS50 == 0 ? new BigDecimal(0) : monthAmountS50.divide(new BigDecimal(monthCountS50), 2, RoundingMode.HALF_UP);
        BigDecimal monthAvgAmountL50 = monthCountL50 == 0 ? new BigDecimal(0) : monthAmountL50.divide(new BigDecimal(monthCountL50), 2, RoundingMode.HALF_UP);
        BigDecimal monthAvgAmountD50 = monthCountD50 == 0 ? new BigDecimal(0) : monthAmountD50.divide(new BigDecimal(monthCountD50), 2, RoundingMode.HALF_UP);
        BigDecimal monthAvgAmountD55 = monthCountD55 == 0 ? new BigDecimal(0) : monthAmountD55.divide(new BigDecimal(monthCountD55), 2, RoundingMode.HALF_UP);
        BigDecimal monthAvgAmountY50 = monthCountY50 == 0 ? new BigDecimal(0) : monthAmountY50.divide(new BigDecimal(monthCountY50), 2, RoundingMode.HALF_UP);
        BigDecimal monthAvgAmountZ50 = monthCountZ50 == 0 ? new BigDecimal(0) : monthAmountZ50.divide(new BigDecimal(monthCountZ50), 2, RoundingMode.HALF_UP);
        BigDecimal monthAvgAmountH55 = monthCountH55 == 0 ? new BigDecimal(0) : monthAmountH55.divide(new BigDecimal(monthCountH55), 2, RoundingMode.HALF_UP);

        //本月平均客户交易笔数
        Integer monthAvgCountP40 = merchantMonthCountP40 == 0 ? 0 : monthCountP40 / merchantMonthCountP40;
        Integer monthAvgCountP42 = merchantMonthCountP42 == 0 ? 0 : monthCountP42 / merchantMonthCountP42;
        Integer monthAvgCountP45 = merchantMonthCountP45 == 0 ? 0 : monthCountP45 / merchantMonthCountP45;
        Integer monthAvgCountS40 = merchantMonthCountS40 == 0 ? 0 : monthCountS40 / merchantMonthCountS40;
        Integer monthAvgCountL40 = merchantMonthCountL40 == 0 ? 0 : monthCountL40 / merchantMonthCountL40;
        Integer monthAvgCountD40 = merchantMonthCountD40 == 0 ? 0 : monthCountD40 / merchantMonthCountD40;
        Integer monthAvgCountM40 = merchantMonthCountM40 == 0 ? 0 : monthCountM40 / merchantMonthCountM40;
        Integer monthAvgCountK40 = merchantMonthCountK40 == 0 ? 0 : monthCountK40 / merchantMonthCountK40;
        Integer monthAvgCountY40 = merchantMonthCountY40 == 0 ? 0 : monthCountY40 / merchantMonthCountY40;
        Integer monthAvgCountP50 = merchantMonthCountP50 == 0 ? 0 : monthCountP50 / merchantMonthCountP50;
        Integer monthAvgCountS50 = merchantMonthCountS50 == 0 ? 0 : monthCountS50 / merchantMonthCountS50;
        Integer monthAvgCountL50 = merchantMonthCountL50 == 0 ? 0 : monthCountL50 / merchantMonthCountL50;
        Integer monthAvgCountD50 = merchantMonthCountD50 == 0 ? 0 : monthCountD50 / merchantMonthCountD50;
        Integer monthAvgCountD55 = merchantMonthCountD55 == 0 ? 0 : monthCountD55 / merchantMonthCountD55;
        Integer monthAvgCountY50 = merchantMonthCountY50 == 0 ? 0 : monthCountY50 / merchantMonthCountY50;
        Integer monthAvgCountZ50 = merchantMonthCountZ50 == 0 ? 0 : monthCountZ50 / merchantMonthCountZ50;
        Integer monthAvgCountH55 = merchantMonthCountH55 == 0 ? 0 : monthCountH55 / merchantMonthCountH55;

        String content = "【交易统计】\n\n";
        content += "【交易日统计】\n";
        content += "【银小云" + getYesterdayByCalendar() + "交易统计】\n";
        content += "【基梯/P40/P45/S40/L40/D40/M40/K40】\n\n";
        content += "【基梯交易总额：" + dayAmountJT + "元】\n";
        content += "【基梯交易总笔数：" + dayCountJT + "笔】\n\n";
        content += "【P40交易总额：" + dayAmountP40 + "元】\n";
        content += "【P40交易总笔数：" + dayCountP40 + "笔】\n";
        content += "【P40平均每笔交易金额：" + dayAvgAmountP40 + "元】\n";
        content += "【P40平均商户交易笔数：" + dayAvgCountP40 + "笔】\n\n";
        content += "【P42交易总额：" + dayAmountP42 + "元】\n";
        content += "【P42交易总笔数：" + dayCountP42 + "笔】\n";
        content += "【P42平均每笔交易金额：" + dayAvgAmountP42 + "元】\n";
        content += "【P42平均商户交易笔数：" + dayAvgCountP42 + "笔】\n\n";
        content += "【P45交易总额：" + dayAmountP45 + "元】\n";
        content += "【P45交易总笔数：" + dayCountP45 + "笔】\n";
        content += "【P45平均每笔交易金额：" + dayAvgAmountP45 + "元】\n";
        content += "【P45平均商户交易笔数：" + dayAvgCountP45 + "笔】\n\n";
        content += "【S40交易总额：" + dayAmountS40 + "元】\n";
        content += "【S40交易总笔数：" + dayCountS40 + "笔】\n";
        content += "【S40平均每笔交易金额：" + dayAvgAmountS40 + "元】\n";
        content += "【S40平均商户交易笔数：" + dayAvgCountS40 + "笔】\n\n";
        content += "【L40交易总额：" + dayAmountL40 + "元】\n";
        content += "【L40交易总笔数：" + dayCountL40 + "笔】\n";
        content += "【L40平均每笔交易金额：" + dayAvgAmountL40 + "元】\n";
        content += "【L40平均商户交易笔数：" + dayAvgCountL40 + "笔】\n\n";
        content += "【D40交易总额：" + dayAmountD40 + "元】\n";
        content += "【D40交易总笔数：" + dayCountD40 + "笔】\n";
        content += "【D40平均每笔交易金额：" + dayAvgAmountD40 + "元】\n";
        content += "【D40平均商户交易笔数：" + dayAvgCountD40 + "笔】\n\n";
        content += "【K40交易总额：" + dayAmountK40 + "元】\n";
        content += "【K40交易总笔数：" + dayCountK40 + "笔】\n";
        content += "【K40平均每笔交易金额：" + dayAvgAmountK40 + "元】\n";
        content += "【K40平均商户交易笔数：" + dayAvgCountK40 + "笔】\n\n";
        content += "【Y40交易总额：" + dayAmountY40 + "元】\n";content += "【Y40交易总笔数：" + dayCountY40 + "笔】\n";content += "【Y40平均每笔交易金额：" + dayAvgAmountY40 + "元】\n";content += "【Y40平均商户交易笔数：" + dayAvgCountY40 + "笔】\n\n";
        content += "【P50交易总额：" + dayAmountP50 + "元】\n";content += "【P50交易总笔数：" + dayCountP50 + "笔】\n";content += "【P50平均每笔交易金额：" + dayAvgAmountP50 + "元】\n";content += "【P50平均商户交易笔数：" + dayAvgCountP50 + "笔】\n\n";
        content += "【S50交易总额：" + dayAmountS50 + "元】\n";content += "【S50交易总笔数：" + dayCountS50 + "笔】\n";content += "【S50平均每笔交易金额：" + dayAvgAmountS50 + "元】\n";content += "【S50平均商户交易笔数：" + dayAvgCountS50 + "笔】\n\n";
        content += "【L50交易总额：" + dayAmountL50 + "元】\n";content += "【L50交易总笔数：" + dayCountL50 + "笔】\n";content += "【L50平均每笔交易金额：" + dayAvgAmountL50 + "元】\n";content += "【L50平均商户交易笔数：" + dayAvgCountL50 + "笔】\n\n";
        content += "【D50交易总额：" + dayAmountD50 + "元】\n";content += "【D50交易总笔数：" + dayCountD50 + "笔】\n";content += "【D50平均每笔交易金额：" + dayAvgAmountD50 + "元】\n";content += "【D50平均商户交易笔数：" + dayAvgCountD50 + "笔】\n\n";
        content += "【D55交易总额：" + dayAmountD55 + "元】\n";content += "【D55交易总笔数：" + dayCountD55 + "笔】\n";content += "【D55平均每笔交易金额：" + dayAvgAmountD55 + "元】\n";content += "【D55平均商户交易笔数：" + dayAvgCountD55 + "笔】\n\n";
        content += "【Y50交易总额：" + dayAmountY50 + "元】\n";content += "【Y50交易总笔数：" + dayCountY50 + "笔】\n";content += "【Y50平均每笔交易金额：" + dayAvgAmountY50 + "元】\n";content += "【Y50平均商户交易笔数：" + dayAvgCountY50 + "笔】\n\n";
        content += "【Z50交易总额：" + dayAmountZ50 + "元】\n";content += "【Z50交易总笔数：" + dayCountZ50 + "笔】\n";content += "【Z50平均每笔交易金额：" + dayAvgAmountZ50 + "元】\n";content += "【Z50平均商户交易笔数：" + dayAvgCountZ50 + "笔】\n\n";
        content += "【H55交易总额：" + dayAmountH55 + "元】\n";content += "【H55交易总笔数：" + dayCountH55 + "笔】\n";content += "【H55平均每笔交易金额：" + dayAvgAmountH55 + "元】\n";content += "【H55平均商户交易笔数：" + dayAvgCountH55 + "笔】\n\n";
        content += "【交易统计】【M40】" + getLast2DaysByCalendar() + "\n";
        content += "【M40交易总额：" + dayAmountM40 + "元】\n";
        content += "【M40交易总笔数：" + dayCountM40 + "笔】\n";
        content += "【M40平均每笔交易金额：" + dayAvgAmountM40 + "元】\n";
        content += "【M40平均商户交易笔数：" + dayAvgCountM40 + "笔】\n\n";
        content += "【交易月统计】\n";
        content += "【银小云" + getMonthByCalendar() + "交易统计】\n";
        content += "【基梯/P40/P45/S40/L40/D40/M40/K40】\n\n";
        content += "【基梯交易总额：" + monthAmountJT + "元】\n";
        content += "【基梯交易总笔数：" + monthCountJT + "笔】\n\n";
        content += "【P40交易总额：" + monthAmountP40 + "元】\n";
        content += "【P40交易总笔数：" + monthCountP40 + "笔】\n";
        content += "【P40平均每笔交易金额：" + monthAvgAmountP40 + "元】\n";
        content += "【P40平均商户交易笔数：" + monthAvgCountP40 + "笔】\n\n";
        content += "【P42交易总额：" + monthAmountP42 + "元】\n";
        content += "【P42交易总笔数：" + monthCountP42 + "笔】\n";
        content += "【P42平均每笔交易金额：" + monthAvgAmountP42 + "元】\n";
        content += "【P42平均商户交易笔数：" + monthAvgCountP42 + "笔】\n\n";
        content += "【P45交易总额：" + monthAmountP45 + "元】\n";
        content += "【P45交易总笔数：" + monthCountP45 + "笔】\n";
        content += "【P45平均每笔交易金额：" + monthAvgAmountP45 + "元】\n";
        content += "【P45平均商户交易笔数：" + monthAvgCountP45 + "笔】\n\n";
        content += "【S40交易总额：" + monthAmountS40 + "元】\n";
        content += "【S40交易总笔数：" + monthCountS40 + "笔】\n";
        content += "【S40平均每笔交易金额：" + monthAvgAmountS40 + "元】\n";
        content += "【S40平均商户交易笔数：" + monthAvgCountS40 + "笔】\n\n";
        content += "【L40交易总额：" + monthAmountL40 + "元】\n";
        content += "【L40交易总笔数：" + monthCountL40 + "笔】\n";
        content += "【L40平均每笔交易金额：" + monthAvgAmountL40 + "元】\n";
        content += "【L40平均商户交易笔数：" + monthAvgCountL40 + "笔】\n\n";
        content += "【D40交易总额：" + monthAmountD40 + "元】\n";
        content += "【D40交易总笔数：" + monthCountD40 + "笔】\n";
        content += "【D40平均每笔交易金额：" + monthAvgAmountD40 + "元】\n";
        content += "【D40平均商户交易笔数：" + monthAvgCountD40 + "笔】\n\n";
        content += "【M40交易总额：" + monthAmountM40 + "元】\n";
        content += "【M40交易总笔数：" + monthCountM40 + "笔】\n";
        content += "【M40平均每笔交易金额：" + monthAvgAmountM40 + "元】\n";
        content += "【M40平均商户交易笔数：" + monthAvgCountM40 + "笔】\n\n";
        content += "【K40交易总额：" + monthAmountK40 + "元】\n";
        content += "【K40交易总笔数：" + monthCountK40 + "笔】\n";
        content += "【K40平均每笔交易金额：" + monthAvgAmountK40 + "元】\n";
        content += "【K40平均商户交易笔数：" + monthAvgCountK40 + "笔】\n\n";
        content += "【Y40交易总额：" + monthAmountY40 + "元】\n";content += "【Y40交易总笔数：" + monthCountY40 + "笔】\n";content += "【Y40平均每笔交易金额：" + monthAvgAmountY40 + "元】\n";content += "【Y40平均商户交易笔数：" + monthAvgCountY40 + "笔】\n\n";
        content += "【P50交易总额：" + monthAmountP50 + "元】\n";content += "【P50交易总笔数：" + monthCountP50 + "笔】\n";content += "【P50平均每笔交易金额：" + monthAvgAmountP50 + "元】\n";content += "【P50平均商户交易笔数：" + monthAvgCountP50 + "笔】\n\n";
        content += "【S50交易总额：" + monthAmountS50 + "元】\n";content += "【S50交易总笔数：" + monthCountS50 + "笔】\n";content += "【S50平均每笔交易金额：" + monthAvgAmountS50 + "元】\n";content += "【S50平均商户交易笔数：" + monthAvgCountS50 + "笔】\n\n";
        content += "【L50交易总额：" + monthAmountL50 + "元】\n";content += "【L50交易总笔数：" + monthCountL50 + "笔】\n";content += "【L50平均每笔交易金额：" + monthAvgAmountL50 + "元】\n";content += "【L50平均商户交易笔数：" + monthAvgCountL50 + "笔】\n\n";
        content += "【D50交易总额：" + monthAmountD50 + "元】\n";content += "【D50交易总笔数：" + monthCountD50 + "笔】\n";content += "【D50平均每笔交易金额：" + monthAvgAmountD50 + "元】\n";content += "【D50平均商户交易笔数：" + monthAvgCountD50 + "笔】\n\n";
        content += "【D55交易总额：" + monthAmountD55 + "元】\n";content += "【D55交易总笔数：" + monthCountD55 + "笔】\n";content += "【D55平均每笔交易金额：" + monthAvgAmountD55 + "元】\n";content += "【D55平均商户交易笔数：" + monthAvgCountD55 + "笔】\n\n";
        content += "【Y50交易总额：" + monthAmountY50 + "元】\n";content += "【Y50交易总笔数：" + monthCountY50 + "笔】\n";content += "【Y50平均每笔交易金额：" + monthAvgAmountY50 + "元】\n";content += "【Y50平均商户交易笔数：" + monthAvgCountY50 + "笔】\n\n";
        content += "【Z50交易总额：" + monthAmountZ50 + "元】\n";content += "【Z50交易总笔数：" + monthCountZ50 + "笔】\n";content += "【Z50平均每笔交易金额：" + monthAvgAmountZ50 + "元】\n";content += "【Z50平均商户交易笔数：" + monthAvgCountZ50 + "笔】\n\n";
        content += "【H55交易总额：" + monthAmountH55 + "元】\n";content += "【H55交易总笔数：" + monthCountH55 + "笔】\n";content += "【H55平均每笔交易金额：" + monthAvgAmountH55 + "元】\n";content += "【H55平均商户交易笔数：" + monthAvgCountH55 + "笔】\n\n";
        SendDingDingDTO dingDingDTO = new SendDingDingDTO();
        dingDingDTO.setContent(content);
        dingDingDTO.setToken(acmGlobalParams.getDingdingstatisticsToken());
        mcenterRpcDingDingService.send(dingDingDTO);
        XxlJobLogger.log("【钉钉推送银小云机具统计线程】本次推送的内容为:{}", content);
    }

    /**
     * 获取昨日 yyyy年MM月dd日
     */
    public static String getYesterdayByCalendar() {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DATE, -1);
        Date time = calendar.getTime();
        return new SimpleDateFormat("yyyy年MM月dd日").format(time);
    }

    /**
     * 获取前日 yyyy年MM月dd日
     */
    public static String getLast2DaysByCalendar() {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DATE, -2);
        Date time = calendar.getTime();
        return new SimpleDateFormat("yyyy年MM月dd日").format(time);
    }

    /**
     * 获取当月 yyyy年MM月
     */
    public static String getMonthByCalendar() {
        Calendar c = Calendar.getInstance();
        c.add(Calendar.MONTH, 0);
        SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月");
        return format.format(c.getTime());
    }
}
